﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using EntityObjects;
using System.Net;
namespace BAL.Controller
{
    public class Email
    {
        MailMessage m;
        private string to;
        private string message;
        private List<Catalogue> tablelst;
        private string sbj;
        string Toid = "";

      
        public Email(string to) {
            this.to = to;
            sbj = "Department Head Authority Delegate";
            this.message = "<font size=\"3\" face=\"Arial\">Dear "+ to +"<br>" +
                           "By means of this letter, I delegate the authority herein described to you, on the following terms and conditions:<br>" +
                           "<ul><li>The delegate may review approve/reject, on my behalf,  requisition by department employee.</li>" +
                           "<li>The effective date of this delegation shall run until revoked by delegating official.</li>" +
                           "<li>The authority delegated is not subject to sub-delegation without my prior and express written consent.</li></ul></font>" +
                           "<br><br><br><font size=\"3\" face=\"Arial\" color=\"grey\">**********************************************************************" +
                           "<br>This is a Computer Generated Email. Please Do Not Reply to this Email Address. Thank You!" +
                           "<br>**********************************************************************</br></font>";
        }


        //TNS updated for Department Head Authority Email
        public Email(Employee eObj)
        {   
          
           if (eObj != null) { Toid = Convert.ToString(eObj.RoleId); }
          
           if (Toid == "2")
           {

                  this.to=eObj.Email.ToString();
                  this.sbj = "Department Head Authority Delegation";
                  this.message = "<font size=\"3\" face=\"Arial\">Dear " + to + "<br>" +
                                 "By means of this letter, I delegate the authority herein described to you, on the following terms and conditions:<br>" +
                                 "<ul><li>The delegate may review approve/reject, on my behalf,  requisition by department employee.</li>" +
                                 "<li>The effective date of this delegation shall run until revoked by delegating official.</li>" +
                                 "<li>The authority delegated is not subject to sub-delegation without my prior and express written consent.</li></ul></font>" +
                                 "<br><br><br><font size=\"3\" face=\"Arial\" color=\"grey\">**********************************************************************" +
                                 "<br>This is a Computer Generated Email. Please Do Not Reply to this Email Address. Thank You!" +
                                 "<br>**********************************************************************</br></font>";
           }

           //To send email to Remove Dept Head authority person               
           else
           {

                  if (eObj != null) { this.to = eObj.Email.ToString(); }
                  this.sbj = "Remove Department Head Authority";
                  this.message = "<font size=\"3\" face=\"Arial\">Dear Receiver,<br>" +
                                 "This is to inform that authorised head authority eariler assigned to you is no longer effect today onwards." +
                                 "We are very appreciated on your contributed service."+
                                 "<br><br><br><font size=\"3\" face=\"Arial\" color=\"grey\">**********************************************************************" +
                                 "<br>This is a Computer Generated Email. Please Do Not Reply to this Email Address. Thank You!" +
                                 "<br>**********************************************************************</br></font>";
               
           }
 

        }


        //Send email after Assign Department Representative
        public Email(Employee eObj,string Collectionpint)
        {
            if (eObj != null) { Toid = Convert.ToString(eObj.RoleId); }


            if (Toid == "3")
            {
                this.to = eObj.Email.ToString();
                this.sbj = "Assigned Department Representative Announcement.";
                this.message = "<font size=\"3\" face=\"Arial\">Dear Receiver,<br>" +
                              "This is to inform that following person are assigned as Department Representative for Stationery Collection process from Logic Stationery System." +
                              "The effective date of this responsibility shall run until revoked by Department manager." +
                               "<br>Name :" + eObj.EmpName + "<br>" +
                              "Collection Point :" + Collectionpint + "</font>" +
                              "<br><br><br><font size=\"3\" face=\"Arial\" color=\"grey\">**********************************************************************" +
                              "<br>This is a Computer Generated Email. Please Do Not Reply to this Email Address. Thank You!" +
                              "<br>**********************************************************************</br></font>";
            }
            else
            {
                if (eObj != null) { this.to = eObj.Email.ToString(); }
                this.to = eObj.Email.ToString();
                this.sbj = "Replace Department Representative Announcement.";
                this.message = "<font size=\"3\" face=\"Arial\">Dear Receiver,<br>" +
                                 "This is to inform that Department Representative responsibility eariler assigned to you is no longer effect today onwards." +
                                 "We are very appreciated on your contributed service." +
                                 "<br><br><br><font size=\"3\" face=\"Arial\" color=\"grey\">**********************************************************************" +
                                 "<br>This is a Computer Generated Email. Please Do Not Reply to this Email Address. Thank You!" +
                                 "<br>**********************************************************************</br></font>";
                                

            }
         

        }


        public Email(string status, string to) {
            this.to = to;
            this.sbj = "Inform Requisition Status";
            this.message = "<font size=\"3\" face=\"Arial\">Your requisition has been "+status+" for more details login to your system</font>"+
                "<br><br><br><font size=\"3\" face=\"Arial\" color=\"grey\">**********************************************************************" +
                           "<br>This is a Computer Generated Email. Please Do Not Reply to this Email Address. Thank You!" +
                           "<br>**********************************************************************</br></font>";            
        }

       

        
        public Email(int i,string to) {
            this.to = to;
            this.sbj = "Pending Requisition Forms";
            this.message = "<font size=\"3\" face=\"Arial\">There are " + i + " pending requisition forms to approve.</font>" + "<br><br><br><br><font size=\"3\" face=\"Arial\" color=\"grey\">"
                +"**********************************************************************" +
                "<br>This is a Computer Generated Email. Please Do Not Reply to this Email Address. Thank You!" +
                "<br>**********************************************************************</br></font>";
        }

        public Email(string deptName,string newColPoint,string to) {
            this.to = to;
            this.sbj = "Change Collection Point Announcement";
            this.message = "<font size=\"3\" face=\"Arial\">Collection Point  has updated to " + newColPoint + "for" + deptName + "deptmart.</font>" + "<br><br><br><br><font size=\"3\" face=\"Arial\" color=\"grey\">"
                             + "**********************************************************************" +
                                "<br>This is a Computer Generated Email. Please Do Not Reply to this Email Address. Thank You!" +
                               "<br>**********************************************************************</br></font>";
        }

        public Email(string to,string message,List<Catalogue> tablelst) {
            this.to = to;
            this.message = message;           
            this.tablelst = new List<Catalogue>();
            this.tablelst.AddRange(tablelst);
        }                
        public void createMail(){
        System.Net.Mail.MailMessage me = new System.Net.Mail.MailMessage();
            me.To.Add(to);
            me.Subject =sbj;
            me.From = new System.Net.Mail.MailAddress("sa34team12ad@gmail.com");
            me.IsBodyHtml = true;
            me.Body = message;
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
            try
			{
                smtp.Host = "lynx.iss.nus.edu.sg";
                smtp.Send(me);              
			}
			catch (SmtpFailedRecipientsException ex)
			{
				for (int i = 0; i < ex.InnerExceptions.Length; i++)
				{
					SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;
					if (status == SmtpStatusCode.MailboxBusy ||
						status == SmtpStatusCode.MailboxUnavailable)
					{
						Console.WriteLine("Delivery failed - retrying in 5 seconds.");
						System.Threading.Thread.Sleep(5000);
						smtp.Send(me);
					}
					else
					{
						Console.WriteLine("Failed to deliver message to {0}", 
						    ex.InnerExceptions[i].FailedRecipient);
					}
				}
			}
            catch (Exception ex)
            {
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587; //for gmail
                smtp.EnableSsl = true;//SSL (true, false)
                smtp.Credentials = new NetworkCredential("sa34team12ad@gmail.com", "sa34team12ad");
                smtp.Send(me); 
                
            }      
            
            smtp.Dispose();
        }
        

}
}